//
//  PostView.m
//  项目一TimeMovie
//
//  Created by apple on 16/4/5.
//  Copyright © 2016年 huangmingyu. All rights reserved.
//

#import "PostView.h"
#import "HomeModel.h"

@implementation PostView

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

- (instancetype)init
{

    return [self initWithFrame:CGRectZero];
}
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self creatUI];
    }
    return self;
}
- (void)awakeFromNib {
    
    [super awakeFromNib];
    [self creatUI];
    
}

- (void)creatUI {
    
    //创建海报视图
    [self creatLargeView];
    
    //创建头部视图
    [self creatHeaderView];
    
    //创建底部视图
    [self creatBottomView];
    //创建灯光视图
    [self creatLightView];
    
    [self bringSubviewToFront:headView];
    
    
    [self creatMaskView];
    [self insertSubview:maskView belowSubview:headView];
    
    [self creatGesture];
    
    [largeView addObserver:smallView forKeyPath:MovieKVOValue options:NSKeyValueObservingOptionNew context:nil];
    [smallView addObserver:largeView forKeyPath:MovieKVOValue options:NSKeyValueObservingOptionNew context:nil];
    [largeView addObserver:self forKeyPath:MovieKVOValue options:NSKeyValueObservingOptionNew context:nil];
}

- (void)setDataArray:(NSArray *)dataArray {
    
    _dataArray = dataArray;
    largeView.dataArray = _dataArray;
    smallView.dataArray = _dataArray;
    [self changeTitleWithIndex:0];
    
}

- (void)creatLargeView {

    MovieLayout *layout = [[LargeLayout alloc] init];
    largeView = [[LagreCollectionView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight - 64 - 49 - kFooterHeight) collectionViewLayout:layout];
    
    [self addSubview:largeView];
    
}

- (void)creatHeaderView {
    
    UIImage *image = [UIImage imageNamed:@"indexBG_home"];
    image = [image stretchableImageWithLeftCapWidth:0 topCapHeight:2];
    headView = [[UIImageView alloc] initWithImage:image];
    headView.frame = CGRectMake(0, - kMovieSmallViewHeight - 5 , kScreenWidth, kHeaderHeight);
    headView.userInteractionEnabled = YES;
    
    [self addSubview:headView];
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(kScreenWidth / 2 - 26 /2 , kHeaderHeight - 20, 26, 20);
    [button setImage:[UIImage imageNamed:@"down_home"] forState:UIControlStateNormal];
    [button setImage:[UIImage imageNamed:@"up_home"] forState:UIControlStateSelected];
    button.tag = 100;
    [button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    
    [headView addSubview:button];
    
    MovieLayout *layout = [[SmallLayout alloc] init];
    smallView = [[SmallCollectionView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kMovieSmallViewHeight) collectionViewLayout:layout];
    [headView addSubview:smallView];
    
    
}

- (void)creatBottomView {
    
    titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, kScreenHeight - 64 - 49 - kFooterHeight, kScreenWidth, kFooterHeight)];
    titleLabel.textAlignment = NSTextAlignmentCenter;
    titleLabel.text = @"火车人";
    titleLabel.font = [UIFont systemFontOfSize:20];
    titleLabel.textColor = [UIColor whiteColor];
    titleLabel.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"poster_title_home"]];
    [self addSubview:titleLabel];
}

- (void)creatLightView {
    
    UIImageView *left = [[UIImageView alloc] initWithFrame:CGRectMake(10, 5, 124, 204)];
    left.image = [UIImage imageNamed:@"light"];
    
    UIImageView *right = [[UIImageView alloc] initWithFrame:CGRectMake(kScreenWidth - 10 - 124, 5, 124, 204)];
    right.image = [UIImage imageNamed:@"light"];
    
    [self addSubview:left];
    [self addSubview:right];
    
}

- (void)creatGesture {
    
    UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(maskAction)];
    swipe.direction = UISwipeGestureRecognizerDirectionDown;
    
    [self addGestureRecognizer:swipe];
}


- (void)creatMaskView {
    
    maskView = [[UIControl alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight - 49 - 64 - 35)];
    [maskView addTarget:self action:@selector(maskAction) forControlEvents:UIControlEventTouchUpInside];
    maskView.backgroundColor = [UIColor blackColor];
    maskView.alpha = 0;
    
    [self addSubview:maskView];
    
}

- (void)buttonAction:(UIButton *)button {
    
    button.selected = !button.selected;
    //界面动画
    [UIView animateWithDuration:0.25 animations:^{
       
        headView.top = button.selected ? 0 : (-kMovieSmallViewHeight - 5);
        maskView.alpha = button.selected ? 0.6 : 0;
    }];
    
    
}

- (void)maskAction {
    
    UIButton *button = [headView viewWithTag:100];
    
    [self buttonAction:button];
    
    
}

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *,id> *)change context:(void *)context {
    
    NSInteger index = [change[@"new"] integerValue];
    [self changeTitleWithIndex:index];
} 


- (void)changeTitleWithIndex:(NSInteger)index {
    
    HomeModel *model = _dataArray[index];
    titleLabel.text = model.tiltle;
}



@end
